package controller

import (
	"douyin/models"
	"douyin/repository/mysql"
	"douyin/service"
	"douyin/utils/pack"
	"strconv"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// FavoriteAction 赞操作
func FavoriteAction(c *gin.Context) {

	// 1. 获取querystring参数和参数校验
	p := new(models.FavoriteRequest)
	if err := c.ShouldBind(&p); err != nil {
		// 请求参数有误，直接返回响应
		zap.L().Error("controller.FavoriteAction with invalid param", zap.Error(err))
		pack.ResponseError(c, models.CodeInvalidParam)
		return
	}
	//2.业务处理
	userId, _ := c.Get(pack.CtxUserIDKey)
	err := service.FavoriteAction(p, userId.(int64))
	if err != nil {
		zap.L().Error("service.FavoriteAction failed", zap.Error(err))
		pack.ResponseError(c, models.CodeFavoriteActionError)
		return
	}
	//3.返回响应
	pack.ResponseSuccess(c, models.CodeSuccess)
}

// FavoriteList 点赞视频列表
func FavoriteList(c *gin.Context) {
	//1.业务处理
	userId, _ := c.Get(pack.CtxUserIDKey)                      //登录用户id
	otherId, _ := strconv.ParseInt(c.Query("user_id"), 10, 64) //对方用户id

	videoList, err := service.FavoriteList(userId.(int64), otherId)
	//查询空处理
	if err == mysql.ErrorQueryZeroRecord {
		zap.L().Error("service.FavoriteList ErrorQueryZeroRecord!! ", zap.Error(err))
		models.ResponseVideoListSuccess(c, nil)
		return
	}
	if err != nil {
		zap.L().Error("service.FavoriteList failed", zap.Error(err))
		models.ResponseVideoListError(c, models.CodeGetVideoListError)
		return
	}
	//2.返回响应
	models.ResponseVideoListSuccess(c, videoList)
}
